package com.mentorgen.tools.profile.instrument;

import java.util.ArrayList;
import java.util.List;

import org.objectweb.asm.signature.SignatureVisitor;

public class PrefSignatureVisitor implements SignatureVisitor {

	private int signatureSize = 0;
	private List<String> sigList = new ArrayList<String>();
	private boolean isArray = false;

	@Override
	public SignatureVisitor visitArrayType() {

		isArray = true;
		return this;
	}

	@Override
	public void visitBaseType(char descriptor) {

		// if the visited type is already marked to be an array, adds an array sign to the beginning
		// of it and then continues with identifying what the type of the array is.
		// The calculation is left for the estimator rather than trying to calculate it from the
		// base type.
		if (isArray){
			
			sigList.add("[" + Character.toString(descriptor));
			isArray = false;
			return;
		}else
			sigList.add(Character.toString(descriptor));

		signatureSize = PrefSignatureVisitor.getSigSize(descriptor);

	}
	
	public static int getSigSize(char descriptor){
		switch(descriptor){
		case 'Z':
		case 'B':
			return 1;
		case 'C':
		case 'S':
			return 2;
		case 'I':
		case 'F':
			return 4;
		case 'J':
		case 'D':
			return 8;
		case 'V':
			return 0;
		default:
			System.err.println("Unknown Type");
		}
		return 0;
	}

	public int getSigSize(){
		return signatureSize;
	}

	public List<String> getSigList(){
		return sigList;
	}

	@Override
	public SignatureVisitor visitExceptionType() {
		//System.out.println("ExceptionType Visited!");
		return this;
	}

	@Override
	public void visitFormalTypeParameter(String name) {

		//System.out.println("Formal Type Parameter Visited: [name=" + name + "]");

	}

	@Override
	public SignatureVisitor visitParameterType() {
		//System.out.println("Parameter Type Visited!");
		return this;
	}

	@Override
	public SignatureVisitor visitReturnType() {
		//System.out.println("Return Type Visited!");
		return this;
	}

	@Override
	public void visitTypeVariable(String name) {

		//System.out.println("Type Variable Visited: [name=" + name + "]");

	}

/////////////////    methods not used

	@Override
	public SignatureVisitor visitSuperclass() {
		//System.out.println("SuperClass Visited");
		return this;
	}

	@Override
	public void visitTypeArgument() {
		//System.out.println("Type Argument Visited");
	}

	@Override
	public SignatureVisitor visitTypeArgument(char wildcard) {
		//System.out.println("Type Argument Visited: [wildcard=" + wildcard + "]");
		return this;
	}

	@Override
	public void visitClassType(String name) {
		//System.out.println("Class Type Visited: [name=" + name + "]");
		sigList.add(name);
	}

	@Override
	public void visitEnd() {
		//System.out.println("End Visited");
	}

	@Override
	public void visitInnerClassType(String name) {
		//System.out.println("Inner Class Type Visited: [name=" + name + "]");
	}

	@Override
	public SignatureVisitor visitInterface() {
		//System.out.println("Interface Visited");
		return this;
	}

	@Override
	public SignatureVisitor visitInterfaceBound() {
		//System.out.println("Interface Bound Visited");
		return this;
	}

	@Override
	public SignatureVisitor visitClassBound() {
		//System.out.println("Class Bound Visited");
		return this;
	}
}
